<template>
	<view>
		<u-notify ref="uNotify"></u-notify>
		<u-alert
			title="寝室电量(精确值)"
			type = "info"
			:description="tipText"
			center
			@click="setCopyNum()"
		></u-alert>
		<!-- <u--text align="center" color="#aaaaaa" lineHeight="20px" text="寝室电量(精准值):"></u--text> -->
		<!-- <u--text align="center" bold lines="1" color="#d0c693" lineHeight="20px" :text="tipText" @click="setCopyNum()"></u--text> -->
<!-- 		<view class="cardinfo">
			<view class="text-box">
				<view>
					<text>宿舍电量:</text><text>999.99</text>
				</view>
				<view>
					<text>xiaoquid:</text><text>15698</text>
				</view>
				<view>
					<text>房间号:</text><text>16698</text>
				</view>
				<view>
					<text>校园卡id:</text><text>17999</text>
				</view>
			</view>
		</view> -->
		<!-- <div class="text-title">充值电量</div> -->
		<div class="cardbox">
			<u-row customStyle="margin-top:10px" v-show="true">
				<u-col span="1"></u-col>
				<u-col span="10">
					  <span class="eletext">卡id:{{cardno}} ~ 小区id:{{xiaoquId}} ~ roomid:{{roomid}}</span>
				</u-col>
				<u-col span="1"></u-col>
			</u-row>
			<u-row customStyle="margin-top:10px">
				<u-col span="1"></u-col>
				<u-col span="10">
					  <u--input
					    placeholder="请输入寝室号"
					    border="surround"
					    v-model="room"
					  ></u--input>
				</u-col>
				<u-col span="1"></u-col>
			</u-row>
			<u-row customStyle="margin-top:10px">
				<u-col span="1"></u-col>
				<u-col span="10">
					  <u--input
					    placeholder="请输入校园卡密码"
					    border="surround"
					    v-model="cardPwd"
						password
					  ></u--input>
				</u-col>
				<u-col span="1"></u-col>
			</u-row>
			<u-row customStyle="margin-top:10px">
				<u-col span="1"></u-col>
				<u-col span="10">
					  <u--input
					    placeholder="请输入充值金额"
					    border="surround"
					    v-model="toMoney"
						type="number"
						maxlength="5"
					  ></u--input>
				</u-col>
				<u-col span="1"></u-col>
			</u-row>
			<u-row customStyle="margin-top:10px">
				<u-col span="1"></u-col>
				<u-col span="10">
					  <u-button type="primary" :disabled="isnosub" text="点击充值" @click="toSubmit()"></u-button>
				</u-col>
				<u-col span="1"></u-col>
			</u-row>
			<u-row customStyle="margin-top:10px" v-show="isele">
				<u-col span="1"></u-col>
				<u-col span="10">
					  <span class="eletext">约{{ toMoney/0.525 }}度电</span>
				</u-col>
				<u-col span="1"></u-col>
			</u-row>
		</div>
		<!-- 重度提示 -->
		<u-row customStyle="margin-top: 10px" v-show="true">
			<u-col span="1"></u-col>
			<u-col span="10">
				<u-alert title="请核实待充值的寝室号" center  type = "error"></u-alert>
			</u-col>
			<u-col span="1"></u-col>
		</u-row>
		<u-row customStyle="margin-top: 10px" v-show="true">
			<u-col span="1"></u-col>
			<u-col span="10">
				<u-alert title="请注意:\n扣除的是当前登录账号校园卡内的钱\n到账可能会有延迟,请耐心等待" center  type = "info"></u-alert>
			</u-col>
			<u-col span="1"></u-col>
		</u-row>
		<u-row customStyle="margin-top: 10px">
			<u-col span="1"></u-col>
			<u-col span="10">
				<u-alert title="本服务接口调用自@E安校" center  type = "info"></u-alert>
			</u-col>
			<u-col span="1"></u-col>
		</u-row>
	</view>
</template>

<script>
	import { mixin } from '../../../mixin.js'
	import {AES_Decryptecb} from '../../index/ECB.js'
	import {AES_Encryptecb} from '../ECB.js'
	export default {
		mixins:[mixin],
		data() {
			return {
				isSetRoom:false,
				roomNum:'',//精确值电量
				cardPwd:'',//校园卡密码
				roomid:'',//房间id
				xiaoquId:'',
				cardId:'',
				toMoney:'',//待转金额
				ele:'0',//约电量
				isele:true,
				cardno:'',
				isnosub:false,
				room:''
			};
		},
		computed:{
			tipText(){
				if(!this.isSetRoom){
					return '未设置寝室号'
				}else{
					return this.roomNum.toString()
				}
				
			}
		},
		onLoad() {
			this.getRoomNum()
			this.room = uni.getStorageSync("room")
			this.cardno = uni.getStorageSync("cardno")
			this.xiaoquId = uni.getStorageSync("xiaoquId")
			this.roomid = uni.getStorageSync("roomId")
		},
		methods:{
			//获取寝室电量
			getRoomNum(){
				if(uni.getStorageSync('room')==''){
					this.roomNum = '未设置'
					return
				}
				let roomO = {
					sno: '4122013603',
					xiaoquId: '',
					loudongId: '',
					roomId: '',
					fullroomname: uni.getStorageSync('room'),
				}
				
				uni.request({
					url:'http://vpn.tdhzyun.com/tdwx-fr/ywcl/getRoomList',
					method:"POST",
					headers: {
					    Host: 'vpn.tdhzyun.com',
					    Connection: 'keep-alive',
					    Accept: 'application/json, text/plain, */*',
					    accessToken: '',
					    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36',
					    'Content-Type': 'application/json',
					    Origin: 'http://vpn.tdhzyun.com',
					    Referer: 'http://vpn.tdhzyun.com/person',
					    'Accept-Language': 'zh-CN,zh;q=0.9',
					    'Accept-Encoding': 'gzip, deflate',
					    'Content-Length': '87'
					  },
					  data: {
					    sno: '4122013603',
					    xiaoquId: '',
					    loudongId: '',
					    roomId: '',
					    fullroomname: uni.getStorageSync('room'),
						sign: AES_Encryptecb(JSON.stringify(roomO)+"1234567890ABCDEF"+this.getMd5(JSON.stringify(roomO)))
					  },
					  success:res=>{
						  console.log(res)
						  if(res.statusCode==200){
							  let obj = JSON.parse(this.getAES_Decryptecb(res.data.returnObject)).list[0]
							  if(obj==undefined){return}
							  let num =(obj.allamp - obj.usedamp)
							  this.isSetRoom = true
							  this.roomNum = num
						  }
					  },
					err:e=>{
						console.log(e)
					}
				})
			},
			setCopyNum(){
				uni.setClipboardData({
					data:this.roomNum.toString()
				})
			},
			toSubmit(){
				if(this.room == "" || this.cardPwd == "" || this.toMoney == ""){
					uni.showModal({
						title:'无法充值，某一项为空',
						showCancel:false
					})
					return;
				}
				if(this.xiaoquId == "" || this.cardno == "" || this.roomid == ""){
					uni.showModal({
						title:'上方某一id为空,请重新设置寝室及重新登录',
						showCancel:false
					})
					return;
				}
				let mon = Number(this.toMoney).toFixed(2)
				if(mon == NaN){
					uni.showModal({
						title:'充值金额为非法字符',
						showCancel:false
					})
					return;
				}
				if(mon <=0 || mon > 50){
					uni.showModal({
						title:'单次充值的金额不得低于0元、高于50元',
						showCancel:false
					})
					return;
				}
				uni.showModal({
					title:'确认充值?',
					success:res => {
						if(res.confirm){
							let objw = {
								sno: '4122013603',
								piid: this.cardno,
								passWord: this.cardPwd,
							}
							this.isnosub = true
							uni.request({
								url:'http://vpn.tdhzyun.com/tdwx-fr/ywcl/checkAccountPwd',
								method:"POST",
								headers: {
									Host: 'vpn.tdhzyun.com',
									Connection: 'keep-alive',
									Accept: 'application/json, text/plain, */*',
									accessToken: '',
									'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36',
									'Content-Type': 'application/json',
									Origin: 'http://vpn.tdhzyun.com',
									Referer: 'http://vpn.tdhzyun.com/person',
									'Accept-Language': 'zh-CN,zh;q=0.9',
									'Accept-Encoding': 'gzip, deflate',
									'Content-Length': '87'
								  },
								  data: {
									sno: '4122013603',
									piid: this.cardno,
									passWord: this.cardPwd,
									sign: AES_Encryptecb(JSON.stringify(objw)+"1234567890ABCDEF"+this.getMd5(JSON.stringify(objw)))
								  },
								  success:res=>{
									  console.log(res)
									  if(res.statusCode==200){
										  let obj = JSON.parse(this.getAES_Decryptecb(res.data.returnObject))
										  if(obj==undefined){this.isnosub = false;return}
										  if(obj.msg == "验证成功"){
											  this.saveTranamt()
										  }else{
											  this.isnosub = false;
											  this.cardPwd = ""
											  this.$refs.uNotify.show({
											  	top: 10,
											  	type: 'error',
											  	color: '#000',
											  	bgColor: '#e8e8e8',
											  	message: obj.msg,
											  	duration: 1000 * 3,
											  	fontSize: 20,
											  	safeAreaInsetTop:true
											  })
										  }
									  }
								  },
								err:e=>{
									console.log(e)
								}
							})
						}
					}
				})
				
				return;
			},
			saveTranamt(){
				let objw = {
					sno: '4122013603',
					piid: this.cardno,
					tranamt: this.toMoney,
					xiaoquId:this.xiaoquId,
					roomId:this.roomid
				}
				
				uni.request({
					url:'http://vpn.tdhzyun.com/tdwx-fr/ywcl/saveTranamt',
					method:"POST",
					headers: {
					    Host: 'vpn.tdhzyun.com',
					    Connection: 'keep-alive',
					    Accept: 'application/json, text/plain, */*',
					    accessToken: '',
					    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36',
					    'Content-Type': 'application/json',
					    Origin: 'http://vpn.tdhzyun.com',
					    Referer: 'http://vpn.tdhzyun.com/person',
					    'Accept-Language': 'zh-CN,zh;q=0.9',
					    'Accept-Encoding': 'gzip, deflate',
					    'Content-Length': '87'
					  },
					  data: {
						sno: '4122013603',
						piid: this.cardno,
						tranamt: this.toMoney,
						xiaoquId:this.xiaoquId,
						roomId:this.roomid,
						sign: AES_Encryptecb(JSON.stringify(objw)+"1234567890ABCDEF"+this.getMd5(JSON.stringify(objw)))
					  },
					  success:res=>{
						  console.log(res)
						  if(res.statusCode==200){
							  let obj = JSON.parse(this.getAES_Decryptecb(res.data.returnObject))
							  this.isnosub = false
							  if(obj==undefined){return}
							  this.cardPwd = ""
							  this.$refs.uNotify.show({
								top: 10,
								type: 'error',
								color: '#000',
								bgColor: '#e8e8e8',
								message: obj.msg,
								duration: 1000 * 3,
								fontSize: 20,
								safeAreaInsetTop:true
							  })
							  
						  }
					  },
					err:e=>{
						console.log(e)
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	page{
		// background-color: #55aa7f;
	}
	.cardinfo{
		overflow: hidden;
		width: 720rpx;
		height: 300rpx;
		margin: 0 auto;
		border-radius: 8px;
		border: solid 1px rgba(0, 0, 0, 0.3);
		.text-box{
			margin: 10px 10px 10px 10px;
			&>view{
				line-height: 25px;
			}
		}
	}
	.text-title{
		width: 100%;
		font-size: 16px;
		color: #393939;
		font-weight: bold;
	}
	.cardbox{
		// width: 98%;
		// box-shadow: 0 0 2px rgba(0, 0, 0, 0.3);
		// margin: 0 auto;
	}
	.eletext{
		color: #686868;
		font-size: 12px;
		margin: 0 auto;
	}
</style>
